package com.ehome.poct.core.dao.mybatis;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.poct.core.api.dto.order.TestPoctProfitsRecordDTO;
import com.ehome.poct.core.api.dto.order.UnificationShareAccountDTO;
import com.ehome.poct.core.api.vo.order.UnificationShareAccountVO;
import com.ehome.poct.core.dao.entity.order.TestPoctProfitsRecordDO;
import com.ehome.poct.core.dao.mapping.OrderShareAccountMapping;
import com.ehome.poct.core.dao.mybatis.mapper.order.TestPoctProfitsRecordMapper;
import com.ehome.poct.core.repository.order.TestOrderUnificationPayRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @auther: kuiba.peng
 * @date: 2022/11/22 13:34
 * @description:
 */
@Repository
@AllArgsConstructor
public class TestOrderUnificationPayRepositoryImpl implements TestOrderUnificationPayRepository {

    private final TestPoctProfitsRecordMapper poctProfitsRecordMapper;

    private final OrderShareAccountMapping orderShareAccountMapping;

    private final Sequence sequence;

    @Override
    public UnificationShareAccountVO saveShareAccount(List<UnificationShareAccountDTO> shareAccountDTOList, String orderNo) {
        List<TestPoctProfitsRecordDO> recordDOList = orderShareAccountMapping.toProfitsRecords(shareAccountDTOList);
        recordDOList.forEach(profitsRecordDO -> {
            profitsRecordDO.setOrderNo(orderNo);
            profitsRecordDO.setProfitsRecordId(sequence.nextId());
            profitsRecordDO.setStatus(1);
            poctProfitsRecordMapper.insert(profitsRecordDO);
        });
        List<UnificationShareAccountDTO> accountDTOList = BeanUtil.copyToList(recordDOList, UnificationShareAccountDTO.class);
        return new UnificationShareAccountVO().setOrderNo(orderNo).setUnificationShareAccountDTOList(accountDTOList);
    }

    @Override
    public void delShareAccountByOrderNo(String orderNo) {
        poctProfitsRecordMapper.delete(Wrappers.<TestPoctProfitsRecordDO>lambdaQuery().eq(TestPoctProfitsRecordDO::getOrderNo, orderNo));
    }

    @Override
    public void updateShareAccountByOrderNo(String orderNo, Integer shareAccountStatus) {
        poctProfitsRecordMapper.updateStatusByOrderNo(shareAccountStatus, orderNo);
    }

    @Override
    public List<TestPoctProfitsRecordDTO> getShareAccountList(String orderNo) {
        List<TestPoctProfitsRecordDO> recordDOList = poctProfitsRecordMapper.findByOrderNo(orderNo);
        return BeanUtil.copyToList(recordDOList, TestPoctProfitsRecordDTO.class);
    }
}
